package com.itheima.bos.web.action.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.itheima.bos.bean.base.Standard;
import com.itheima.bos.service.StandardService;
import com.opensymphony.xwork2.ActionContext;

@Controller
@Namespace("/standard")
@Scope("prototype")
@Actions
@ParentPackage("json-default")
public class StandardAction extends BaseAction<Standard> {

	@Autowired
	private StandardService service;

	/**
	 * 分页查询列表
	 */
	@Action(value = "standard_pageQuery", results = { @Result(name = "success", type = "json") })
	public String pageQuery() {
		//调用service
		Pageable pageable = new PageRequest(page - 1, rows);
		Page<Standard> pageData = service.findPageData(pageable);

		//返回json数据
		Map<String, Object> result = new HashMap<String, Object>(2);
		result.put("total", pageData.getTotalElements());
		result.put("rows", pageData.getContent());

		//将map转化成json格式返回,使用struts2-json-plugin
		ActionContext.getContext().getValueStack().push(result);

		return SUCCESS;
	}

	/**
	 * 删除操作
	 */
	private String ids;

	public void setIds(String ids) {
		this.ids = ids;
	}

	@Action(value = "standard_deleteStandard", results = {
			@Result(name = "success", type = "redirect", location = "../pages/base/standard.html") })
	public String deleteStandard() {
		System.out.println(ids);

		String[] idsArr = ids.split(",");
		service.delete(idsArr);

		return SUCCESS;
	}

	@Action(value = "standard_save", results = {
			@Result(name = "success", type = "redirect", location = "../pages/base/standard.html") })
	public String standard_save() {

		service.save(model);

		return SUCCESS;
	}

	@Action(value = "standard_findAll", results = { @Result(name = "success", type = "json") })
	public String standard_findAll() {
		List<Standard> list = service.findAll();
		ActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}

}
